/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClientGUI.java
 *
 * Created on Apr 13, 2011, 8:21:33 PM
 */
package com.swe622.rmi.client.gui;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;


import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.util.List;
import java.util.ArrayList;
import com.swe622.rmi.client.facepicclient.Client;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.Timer;
import java.util.TimerTask;
import java.util.Vector;
import java.util.concurrent.ScheduledExecutorService;
import javax.swing.ImageIcon;

import javax.swing.MutableComboBoxModel;
import shared.*;

/**
 *
 * @author Melanie
 */
public class ClientGUI extends javax.swing.JFrame {

    JButton go;
    JFileChooser chooser;
    JFileChooser saveToLocation;
    String choosertitle;
    String fileContent = "";
    File[] files;
    static final long serialVersionUID = 0;
    private Client client;

    /** Creates new form ClientGUI */
    public ClientGUI() throws FileNotFoundException, IOException, ClassNotFoundException {
        initComponents();

        client = new Client();

        client.startProgressMonitor();
        //get the xfer list model from the client.. client should keep state
        //of everything... GUI will interact with the client
        //client is the "model" in our Model View Controller
        //implementation
        transfersList.setModel(client.getDownloadListModel());
        Timer statusRefreshTimer = new Timer(500, new RefreshTimerHandler());
        statusRefreshTimer.setRepeats(true);
        statusRefreshTimer.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        monitorLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        transfersList = new javax.swing.JList();
        transfersLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        downloadOptionsList = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        UploadLabel = new javax.swing.JLabel();
        BrowseButton = new javax.swing.JButton();
        uploadField = new javax.swing.JTextField();
        BrowseOkButton = new javax.swing.JButton();
        selectServerBox = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        searchLabel = new javax.swing.JLabel();
        searchField = new javax.swing.JTextField();
        SearchButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        DownloadButton = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        LogDisplayArea = new javax.swing.JTextArea();
        downloadOptionsLabel = new javax.swing.JLabel();
        SaveDownloadsToLabel = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        ChangeDownloadLocationButton = new javax.swing.JButton();
        dwnloadPlace = new javax.swing.JLabel();
        resumeButton = new javax.swing.JButton();
        fakeButton = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));

        monitorLabel.setFont(new java.awt.Font("American Typewriter", 1, 18));
        monitorLabel.setForeground(new java.awt.Color(51, 153, 255));
        monitorLabel.setText("Activity:");

        transfersList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                transfersListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(transfersList);

        transfersLabel.setFont(new java.awt.Font("American Typewriter", 1, 18));
        transfersLabel.setForeground(new java.awt.Color(51, 153, 255));
        transfersLabel.setText("Transfers: ");

        jScrollPane2.setViewportView(downloadOptionsList);

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true));

        UploadLabel.setFont(new java.awt.Font("American Typewriter", 1, 18));
        UploadLabel.setForeground(new java.awt.Color(51, 153, 255));
        UploadLabel.setText("Upload File: ");

        BrowseButton.setText("Browse");
        BrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BrowseButtonActionPerformed(evt);
            }
        });

        BrowseOkButton.setText("OK");
        BrowseOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BrowseOkButtonActionPerformed(evt);
            }
        });

        MutableComboBoxModel model = (MutableComboBoxModel)selectServerBox.getModel();
        selectServerBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Server"}));
        selectServerBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectServerBoxActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                        .add(36, 36, 36)
                        .add(selectServerBox, 0, 399, Short.MAX_VALUE))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(24, 24, 24)
                        .add(BrowseButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(uploadField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 277, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(33, 33, 33))
            .add(jPanel1Layout.createSequentialGroup()
                .add(185, 185, 185)
                .add(BrowseOkButton)
                .addContainerGap(236, Short.MAX_VALUE))
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(UploadLabel)
                .addContainerGap(348, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(UploadLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(BrowseButton)
                    .add(uploadField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 13, Short.MAX_VALUE)
                .add(selectServerBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(BrowseOkButton))
        );

        jPanel3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true));

        searchLabel.setFont(new java.awt.Font("American Typewriter", 1, 18));
        searchLabel.setForeground(new java.awt.Color(51, 153, 255));
        searchLabel.setText("Search:");

        SearchButton.setText("Search");
        SearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(searchField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 390, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(searchLabel))
                    .add(jPanel3Layout.createSequentialGroup()
                        .add(165, 165, 165)
                        .add(SearchButton)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(3, 3, 3)
                .add(searchLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(searchField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(SearchButton)
                .addContainerGap(33, Short.MAX_VALUE))
        );

        DownloadButton.setText("Download File");
        DownloadButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                DownloadButtonMouseReleased(evt);
            }
        });

        LogDisplayArea.setColumns(20);
        LogDisplayArea.setRows(5);
        jScrollPane3.setViewportView(LogDisplayArea);

        downloadOptionsLabel.setFont(new java.awt.Font("American Typewriter", 1, 18));
        downloadOptionsLabel.setForeground(new java.awt.Color(51, 153, 255));
        downloadOptionsLabel.setText("Available Downloads:");

        SaveDownloadsToLabel.setFont(new java.awt.Font("American Typewriter", 1, 18));
        SaveDownloadsToLabel.setForeground(new java.awt.Color(51, 153, 255));
        SaveDownloadsToLabel.setText("Save Downloads to:");

        jButton1.setText("jButton1");

        ChangeDownloadLocationButton.setText("Change");
        ChangeDownloadLocationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChangeDownloadLocationButtonActionPerformed(evt);
            }
        });

        dwnloadPlace.setText("No destination selected");

        resumeButton.setText("Resume");
        resumeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resumeButtonActionPerformed(evt);
            }
        });

        fakeButton.setText("fake button");
        fakeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fakeButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(39, 39, 39)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
                            .add(transfersLabel)
                            .add(jPanel2Layout.createSequentialGroup()
                                .add(downloadOptionsLabel)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(DownloadButton)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(resumeButton))
                            .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jLabel4)
                        .add(481, 481, 481)))
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel2Layout.createSequentialGroup()
                                .add(monitorLabel)
                                .add(150, 150, 150)
                                .add(fakeButton))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2Layout.createSequentialGroup()
                                        .add(20, 20, 20)
                                        .add(SaveDownloadsToLabel)
                                        .add(18, 18, 18)
                                        .add(dwnloadPlace, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE))
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .add(486, 486, 486)))
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                        .add(28, 28, 28)
                        .add(ChangeDownloadLocationButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 663, Short.MAX_VALUE)
                        .add(jButton1)
                        .add(123, 123, 123))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 88, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(11, 11, 11)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(SaveDownloadsToLabel)
                            .add(dwnloadPlace))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButton1)
                            .add(ChangeDownloadLocationButton))))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(20, 20, 20)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(monitorLabel)
                            .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(downloadOptionsLabel)
                                .add(DownloadButton)
                                .add(resumeButton))))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(18, 18, 18)
                        .add(fakeButton)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 184, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(transfersLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 204, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jScrollPane3))
                .addContainerGap())
        );

        jPanel4.setBackground(new java.awt.Color(153, 51, 255));

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 792, Short.MAX_VALUE)
        );

        jPanel5.setBackground(new java.awt.Color(153, 51, 255));

        org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 101, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 781, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 991, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BrowseButtonActionPerformed
        // TODO add your handling code here:
        chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle(choosertitle);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setMultiSelectionEnabled(true);

        FileFilter filter = new FileNameExtensionFilter("png", "jpg");
        chooser.setFileFilter(filter);

        /* Disable the "All files" option */
        chooser.setAcceptAllFileFilterUsed(false);

        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            files = chooser.getSelectedFiles();

            /* Loop through all files */
            for (int i = 0; i < files.length; i++) {
                uploadField.setText(files[i].getAbsolutePath());
                System.out.println(files[i].getAbsoluteFile());
            }
        } else {
            System.out.println("No Selection ");
        }

        populateServerNames();

    }//GEN-LAST:event_BrowseButtonActionPerformed

    private void BrowseOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BrowseOkButtonActionPerformed

    
        System.out.println(uploadField.getText());

        //Get the users choice of file to upload
        String fileChoice = uploadField.getText();

        //Get the users choice of server

        //Object serverChoice = (ServerInfo)selectServerBox.getSelectedItem();
        ServerInfo serverChoice = (ServerInfo) selectServerBox.getSelectedItem();
        try {
            client.UploadFileToServer(files[0], serverChoice);
        } catch (RemoteException ex) {
            printToTextArea("error uploading file");
        }
    }//GEN-LAST:event_BrowseOkButtonActionPerformed

    private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchButtonActionPerformed

        /*This is the OK button in the Search Window
         * 
         * When this button is pressed, call client method to ask the
         * repositories for the various servers where that file is located.
         */

        //Create arraylist to hold reply from client  
        List<GuiDataObject> myDownloads = new ArrayList<GuiDataObject>();

        //Get the file name from the textbox
        String searchTxt = searchField.getText();

        if (searchTxt.equals("")) {

            //Tell user they must enter something
            JOptionPane.showMessageDialog(jPanel1, "Please type a file name");

        } else {

            //Ask for list of files/servers from client
            myDownloads = client.QueryRegistryForServers(searchTxt);
        }
        //Now these downloadable files should be displayed to user
        GuiObjectListModel listModel = new GuiObjectListModel(myDownloads);
        downloadOptionsList.setModel(listModel);

        DownloadButton.setEnabled(true);

    }//GEN-LAST:event_SearchButtonActionPerformed

    private void DownloadButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_DownloadButtonMouseReleased
       

        //Just for reference:
        System.out.println(downloadOptionsList.getSelectedValue());

        //Get the file that was selected in the JList
        GuiDataObject myFileChoice = (GuiDataObject) downloadOptionsList.getSelectedValue();

        client.DownloadFileFromServer(myFileChoice);

    }//GEN-LAST:event_DownloadButtonMouseReleased

    private void selectServerBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectServerBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_selectServerBoxActionPerformed

    private void ChangeDownloadLocationButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChangeDownloadLocationButtonActionPerformed

        saveToLocation = new JFileChooser();
        saveToLocation.setCurrentDirectory(new java.io.File("."));
        saveToLocation.setDialogTitle("Downloaded File Location");
        saveToLocation.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        saveToLocation.setMultiSelectionEnabled(false);
        int returnVal = saveToLocation.showSaveDialog(ClientGUI.this);
        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
               File directory = saveToLocation.getSelectedFile();

               dwnloadPlace.setText(directory.getAbsolutePath());
        System.out.print(directory.getAbsolutePath());
               client.saveDirectory(directory.getAbsolutePath());
        }

    }//GEN-LAST:event_ChangeDownloadLocationButtonActionPerformed

    private void transfersListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_transfersListMouseClicked

        //If user double clicks on an item in current transfers
        if (evt.getClickCount() == 2) {

            int index = transfersList.locationToIndex(evt.getPoint());
            System.out.println("Double clicked on Item " + index);
            FileDownloadManager fileToResume = (FileDownloadManager)transfersList.getSelectedValue();

            //If the item has FAILED then user can pick a different
            //server to resume the file download
            if(fileToResume.getStatus() == TransferStatus.STOPPED){

                    String fileName = fileToResume.getFileInfo().getFileName();

                    //Ask for list of files/servers from client
                    List<GuiDataObject> myDownloads = new ArrayList<GuiDataObject>();
                    myDownloads = client.QueryRegistryForServers(fileName);

                    //Now these downloadable files should be displayed to user
                    GuiObjectListModel listModel = new GuiObjectListModel(myDownloads);
                    downloadOptionsList.setModel(listModel);

                    resumeButton.setEnabled(true);
                    DownloadButton.setEnabled(false);
            }

            if(fileToResume.getStatus() == TransferStatus.IN_PROGRESS){

                JOptionPane.showMessageDialog(jPanel1, "Would you like to cancel the download?");

                //call method to halt download
            }

            //If the item is complete...
            if(fileToResume.getStatus() == TransferStatus.COMPLETE){

//                displayPic();

                //Remove from list?


            }

         }


    }//GEN-LAST:event_transfersListMouseClicked

    private void resumeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resumeButtonActionPerformed
        
      
        GuiDataObject serverEntry = (GuiDataObject) downloadOptionsList.getSelectedValue();
        ServerInfo targetServer = serverEntry.getMyServer();
 
            client.resumeThisDownload((FileTransferManager) transfersList.getSelectedValue(), targetServer);
     
    }//GEN-LAST:event_resumeButtonActionPerformed

    private void fakeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fakeButtonActionPerformed

  //      URL photoLocal = new URL(client.getImagePath());
    //    ImageIcon photo1 = new ImageIcon( photoLocal );
  //      JLabel pic = new JLabel(photo1);
        JPanel p = new JPanel(new BorderLayout());
        JFrame picFrame = new JFrame();
    //    p.add(pic);
        picFrame.add(p);
        picFrame.setVisible(true);

    }//GEN-LAST:event_fakeButtonActionPerformed



    public void populateServerNames() {

        //Populate the dropdown with the list of available servers
        //Get list
        List<ServerInfo> upServers = new ArrayList<ServerInfo>();
        upServers = client.RetreiveAvailableServerListFromRegistry();

        //Display in JComboBox: selectServerBox
        MutableComboBoxModel model = (MutableComboBoxModel) selectServerBox.getModel();

        //Clear the current contents
        for(int i=0; i < model.getSize(); i++)
        {
            model.removeElementAt(i);
        }

        //Place each new server into the model
        for (ServerInfo si : upServers) {

            model.addElement(si);
        }

        selectServerBox.setModel(model);

    }

    //Print activity to general message text area
    public void printToTextArea(String message) {

        String outputMessage = message;
        LogDisplayArea.append(outputMessage);
        LogDisplayArea.append("\n");
    }

    public static String ltrim(String source) {
        return source.replaceAll("^\\s+", "");
    }

    /* remove trailing whitespace */
    public static String rtrim(String source) {
        return source.replaceAll("\\s+$", "");
    }

    public class RefreshTimerHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String xfersText = "";

            List<String> errors = client.getErrors();
            for (String error : errors) {
                printToTextArea(error);
            }
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new ClientGUI().setVisible(true);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        });


    }

//    public static void displayPic(){

        //Display photo
//                URL photoLocal = new URL(client.getImagePath());
//                ImageIcon photo1 = new ImageIcon( photoLocal );
//                String completeMessage = "Download Complete";
//                JOptionPane.showMessageDialog( null, completeMessage, completeMessage,
//                        JOptionPane.INFORMATION_MESSAGE, photo1 );
//    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BrowseButton;
    private javax.swing.JButton BrowseOkButton;
    private javax.swing.JButton ChangeDownloadLocationButton;
    private javax.swing.JButton DownloadButton;
    private javax.swing.JTextArea LogDisplayArea;
    private javax.swing.JLabel SaveDownloadsToLabel;
    private javax.swing.JButton SearchButton;
    private javax.swing.JLabel UploadLabel;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel downloadOptionsLabel;
    private javax.swing.JList downloadOptionsList;
    private javax.swing.JLabel dwnloadPlace;
    private javax.swing.JButton fakeButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel monitorLabel;
    private javax.swing.JButton resumeButton;
    private javax.swing.JTextField searchField;
    private javax.swing.JLabel searchLabel;
    private javax.swing.JComboBox selectServerBox;
    private javax.swing.JLabel transfersLabel;
    private javax.swing.JList transfersList;
    private javax.swing.JTextField uploadField;
    // End of variables declaration//GEN-END:variables
}
